<template>
	<view class="content">
		<view class="pass-con">
			<view class="pass-item">
				<view style="width: 181rpx;">手机号</view>
				<view style="flex: 1;">
					<u-input placeholder="请输入手机号" fontSize="25rpx" placeholderStyle="color: #999999" border="none"
						v-model="tel"></u-input>
				</view>
			</view>
			<u-line color="#d8d8d8"></u-line>
			<view class="pass-item">
				<view style="width: 181rpx;">验证码</view>
				<view style="flex: 1;">
					<u-input placeholder="请输入验证码" fontSize="25rpx" placeholderStyle="color: #999999" border="none"
						v-model="smsCode">
						<template slot="suffix">
							<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒后重新获取"></u-code>
							<text @click="getCode" style="color: #0277DE;font-size: 31rpx;">
								{{tips}}
							</text>
						</template>
					</u-input>
				</view>
			</view>
			<u-line color="#d8d8d8"></u-line>
			<view class="pass-item">
				<view style="width: 181rpx;">设置新密码</view>
				<view style="flex: 1;">
					<u-input v-if="passwordAtype" password placeholder="请输入6-18位登录密码" fontSize="27rpx"
						placeholderStyle="color: #999999" border="none" v-model="passwordA">
						<template slot="suffix">
							<u-icon color="#999999" name="eye" @click="passwordAtype = !passwordAtype"></u-icon>
						</template>
					</u-input>
					<u-input v-else placeholder="请输入6-18位登录密码" fontSize="27rpx" placeholderStyle="color: #999999"
						border="none" v-model="passwordA">
						<template slot="suffix">
							<image :src="$IMG_URL+'hide.png'" style="width: 16px;height: 16px;"
								@click="passwordAtype = !passwordAtype">
							</image>
						</template>
					</u-input>
				</view>
			</view>
			<u-line color="#d8d8d8"></u-line>
			<view class="pass-item">
				<view style="width: 181rpx;">确认新密码</view>
				<view style="flex: 1;">
					<u-input v-if="passwordBtype" password placeholder="请再次输入6-18位登录密码" fontSize="27rpx"
						placeholderStyle="color: #999999" border="none" v-model="passwordB">
						<template slot="suffix">
							<u-icon color="#999999" name="eye" @click="passwordBtype = !passwordBtype"></u-icon>
						</template>
					</u-input>
					<u-input v-else placeholder="请再次输入6-18位登录密码" fontSize="27rpx" placeholderStyle="color: #999999"
						border="none" v-model="passwordB">
						<template slot="suffix">
							<image :src="$IMG_URL+'hide.png'" style="width: 16px;height: 16px;"
								@click="passwordBtype = !passwordBtype">
							</image>
							<!-- <u-icon name="../../../static/img/hide.png"
								@click="passwordBtype = !passwordBtype"></u-icon> -->
						</template>
					</u-input>
				</view>
			</view>
		</view>
		<view class="login-but">
			<u-button type="primary" shape="circle" text="确定" @click="submitForm"></u-button>
		</view>
	</view>
</template>

<script>
	import {
		resetPwdApi,
		smsApi
	} from '@/api/user'
	export default {
		data() {
			return {
				passwordBtype: true,
				passwordAtype: true,
				tips: '',
				tel: '',
				smsCode: "",
				passwordA: '',
				passwordB: '',
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			async getCode() {
				if (!this.tel) {
					uni.$u.toast('请输入手机号')
					return
				}
				if (!uni.$u.test.mobile(this.tel)) {
					uni.$u.toast('请输入正确的手机号')
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码',
						mask: true
					})
					const res = await smsApi({
						mobile: this.tel
					})
					uni.hideLoading();
					uni.$u.toast('验证码已发送');
					this.$refs.uCode.start();
				} else {
					uni.$u.toast(this.tips);
				}
			},
			async submitForm() {
				if (!this.tel) {
					uni.$u.toast('请输入手机号')
					return
				}
				if (!uni.$u.test.mobile(this.tel)) {
					uni.$u.toast('请输入正确的手机号')
					return
				}
				if (!this.smsCode) {
					uni.$u.toast('请输入验证码')
					return
				}
				console.log(this.passwordA, this.passwordA.length);
				if (!this.passwordA || this.passwordA.length < 6 || this.passwordA.length > 18) {
					uni.$u.toast('请设置6-18位登录密码')
					return
				}
				if (this.passwordA !== this.passwordB) {
					uni.$u.toast('两次输入的密码不一致')
					return
				}

				uni.showLoading({
					title: '提交中...',
					mask: true
				})
				const res = await resetPwdApi({
					tel: this.tel,
					smsCode: this.smsCode,
					password: this.passwordA

				})
				uni.hideLoading()
				uni.showToast({
					title: '修改成功'
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		width: 100vw;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 23rpx;
	}

	.pass-con {
		width: 688rpx;
		padding: 0 23rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
	}

	.pass-item {
		padding: 31rpx 0;
		font-size: 27rpx;
		color: #000000;
		display: flex;
		align-items: center;
	}

	.login-but {
		width: 688rpx;
		margin-top: 77rpx;

		& ::v-deep .u-button {
			height: 77rpx !important;
			font-size: 31rpx !important;
			font-weight: 500 !important;
		}

		& ::v-deep .u-button__text {

			font-size: 31rpx !important;
			font-weight: 500 !important;
		}

	}
</style>